package com.mktech.taraservice.db;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.List;

import android.content.ContentValues;
import android.database.Cursor;
import android.net.Uri;

import com.mktech.taralib.entity.homefetch.MovieItemVo;
import com.mktech.taraservice.db.base.BaseDBManager;

public class MovieDBManager extends BaseDBManager
{
	public static final String TABLE_NAME = "movie";

	public static final String ITEMID = "ItemID";
	public static final String ASSOCIATIONTYPE = "AssociationType";
	public static final String DESCRIPTION = "Description";
	public static final String DURATION = "Duration";
	public static final String IMAGEURL = "ImageURL";
	public static final String ISNEW = "IsNew";
	public static final String LARGEIMAGEURL = "LargeImageURL";
	public static final String PURCHASETYPE = "PurchaseType";
	public static final String RATING = "Rating";
	public static final String RELEASEDATE = "ReleaseDate";
	public static final String REVIEWS = "Reviews";
	public static final String SERIESID = "SeriesID";
	public static final String SERIESIMAGEURL = "SeriesImageURL";
	public static final String SERIESTITLE = "SeriesTitle";
	public static final String TITLE = "Title";
	public static final String VIEWS = "Views";
	public static final String GENERIES = "generies";

	private static MovieDBManager mInstance;
	private List<MovieItemVo> movieVoList;

	public static final TableAttrs movieAttrs = new TableAttrs()
	{
		{
			addAttr(_ID, "INTEGER primary key autoincrement ");
			addAttr(ITEMID, "TEXT UNIQUE ON CONFLICT REPLACE");
			addAttr(ASSOCIATIONTYPE, "TEXT");
			addAttr(DESCRIPTION, "TEXT");
			addAttr(DURATION, "TEXT");
			addAttr(IMAGEURL, "TEXT");
			addAttr(ISNEW, "TEXT");
			addAttr(LARGEIMAGEURL, "TEXT");
			addAttr(PURCHASETYPE, "TEXT");
			addAttr(RATING, "TEXT");
			addAttr(RELEASEDATE, "TEXT");
			addAttr(REVIEWS, "TEXT");
			addAttr(SERIESIMAGEURL, "TEXT");
			addAttr(SERIESTITLE, "TEXT");
			addAttr(TITLE, "TEXT");
			addAttr(VIEWS, "TEXT");
			addAttr(GENERIES, "TEXT");
		}
	};

	public static MovieDBManager obtain()
	{
		if (mInstance == null)
		{
			mInstance = new MovieDBManager();
		}
		return mInstance;
	}

	private MovieDBManager()
	{
		super();
	}

	public boolean deleteAllMovie()
	{
		int n = delete(TABLE_NAME, null, null);
		if (n > 0)
			return true;
		return false;
	}

	public boolean insertMovieList(List<MovieItemVo> list)
	{
		if (list != null)
		{
			movieVoList = list;
		}
		for (Iterator<MovieItemVo> iterator = list.iterator(); iterator.hasNext();)
		{
			MovieItemVo vo = iterator.next();
			ContentValues value = new ContentValues();
			value.put(ITEMID, vo.getItemID());
			value.put(ASSOCIATIONTYPE, vo.getAssociationType());
			value.put(DESCRIPTION, vo.getDescription());
			value.put(DURATION, vo.getDuration());
			value.put(IMAGEURL, vo.getImageURL());
			value.put(ISNEW, vo.getIsNew());
			value.put(LARGEIMAGEURL, vo.getLargeImageURL());
			value.put(PURCHASETYPE, vo.getPurchaseType());
			value.put(RATING, vo.getRating());
			value.put(RELEASEDATE, vo.getReleaseDate());
			value.put(REVIEWS, vo.getReviews());
			value.put(SERIESIMAGEURL, vo.getSeriesImageURL());
			value.put(SERIESTITLE, vo.getSeriesTitle());
			value.put(TITLE, vo.getTitle());
			value.put(VIEWS, vo.getViews());
			value.put(GENERIES, vo.getGeneries());
			insert(TABLE_NAME, value);
		}
		return true;
	}

	public List<MovieItemVo> getMovieList()
	{
		if (movieVoList != null)
		{
			return movieVoList;
		}
		else
		{
			List<MovieItemVo> movieList = new ArrayList<MovieItemVo>();
			Cursor cursor = query(TABLE_NAME, null, null, null, null);

			if (cursor != null && cursor.getCount() > 0)
			{
				cursor.moveToFirst();
				do
				{
					MovieItemVo vo = constructMovieItemVo(cursor);
					movieList.add(vo);
				} while (cursor.moveToNext());
				cursor.close();
			}
			return movieList;
		}
	}

	private MovieItemVo constructMovieItemVo(Cursor cursor)
	{
		MovieItemVo vo = new MovieItemVo();
		vo.setItemID(cursor.getString(cursor.getColumnIndex(ITEMID)));
		vo.setAssociationType(cursor.getString(cursor.getColumnIndex(ASSOCIATIONTYPE)));
		vo.setDescription(cursor.getString(cursor.getColumnIndex(DESCRIPTION)));
		vo.setDuration(cursor.getString(cursor.getColumnIndex(DURATION)));
		vo.setImageURL(cursor.getString(cursor.getColumnIndex(IMAGEURL)));
		vo.setIsNew(cursor.getString(cursor.getColumnIndex(ISNEW)));
		vo.setLargeImageURL(cursor.getString(cursor.getColumnIndex(LARGEIMAGEURL)));
		vo.setPurchaseType(cursor.getString(cursor.getColumnIndex(PURCHASETYPE)));
		vo.setRating(cursor.getString(cursor.getColumnIndex(RATING)));
		vo.setReleaseDate(cursor.getString(cursor.getColumnIndex(RELEASEDATE)));
		vo.setReviews(cursor.getString(cursor.getColumnIndex(REVIEWS)));
		vo.setSeriesImageURL(cursor.getString(cursor.getColumnIndex(SERIESIMAGEURL)));
		vo.setSeriesTitle(cursor.getString(cursor.getColumnIndex(SERIESTITLE)));
		vo.setTitle(cursor.getString(cursor.getColumnIndex(TITLE)));
		vo.setViews(cursor.getString(cursor.getColumnIndex(VIEWS)));
		vo.setGeneries(cursor.getString(cursor.getColumnIndex(GENERIES)));

		return vo;
	}

}
